
<template>
  <div class="app-container calendar-list-container">
      <template>
          <el-form :model="ruleForm2"
                   :rules="rules2"
                   ref="ruleForm2"
                   label-width="100px"
                   class="demo-ruleForm">
            <el-form-item label="原密码"
                          prop="password">
              <el-input type="password"
                        v-model="ruleForm2.password"
                        auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码"
                          prop="newpassword1">
              <el-input type="password"
                        v-model="ruleForm2.newpassword1"
                        auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码"
                          prop="newpassword2">
              <el-input type="password"
                        v-model="ruleForm2.newpassword2"
                        auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary"
                         @click="submitForm('ruleForm2')">修改
              </el-button>
              <el-button @click="resetForm('ruleForm2')">重置</el-button>
            </el-form-item>
          </el-form>
      </template>
  </div>
</template>


<script>

  export default {
    data() {
      var validatePass = (rule, value, callback) => {
        if (this.ruleForm2.password !== '') {
          if (value !== this.ruleForm2.newpassword1) {
            callback(new Error('两次输入密码不一致!'))
          } else {
            callback()
          }
        } else {
          callback()
        }
      }
      return {
		  id:'',
        switchStatus: '',
        avatarUrl: '',
        show: false,
        ruleForm2: {
          username: '',
          password: '',
          newpassword1: '',
          newpassword2: '',
          avatar: '',
        },
        rules2: {
          password: [{required: true, min: 3, message: '原密码不能为空且不少于3位', trigger: 'change'}],
          newpassword1: [{required: false, min: 3, message: '不少于3位', trigger: 'change'}],
          newpassword2: [{required: false, validator: validatePass, trigger: 'blur'}]
        }
      }
    },
    created() {
    },
    mounted() {
		this.$nextTick(function () {
		   this.getUserName()
		})
    },
    methods: {
		getUserName:function(){
			this.id = this.$store.getters.getUser.user.id
			this.ruleForm2.username = this.$store.getters.getUser.user.name
		},
      submitForm(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
			  var url = 'http://localhost:8080/message/user/changePassword/'+this.id
						+'?password='+this.ruleForm2.password+"&newpassword2="+this.ruleForm2.newpassword2
           this.axios.put(url).then(response => {
              if (response.data.data) {
                this.$notify({
                  title: '成功',
                  message: '修改成功',
                  type: 'success',
                  duration: 2000
                })
               this.$router.push({  //核心语句
                       path:'/',   //跳转的路径
                     })
              } else {
                this.$notify({
                  title: '失败',
                  message: response.data.msg,
                  type: 'error',
                  duration: 2000
                })
              }
            }).catch(() => {
              this.$notify({
                title: '失败',
                message: '修改失败',
                type: 'error',
                duration: 2000
              })
            })
          } else {
            return false
          }
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px!important;
    color: #8c939d!important;
    width: 178px!important;
    height: 178px!important;
    line-height: 178px!important;
    text-align: center!important;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
